--  护国远交
local huguoyuanjiao = fk.CreateSkill {
    name = "mcdh_huguoyuanjiao",
}

huguoyuanjiao:addEffect('viewas',{
    pattern = "slash",
    card_filter = function(self, to_select, selected)
        return #selected == 0 and Fk:getCardById(to_select).type ~= Card.TypeBasic
    end,
    view_as = function(self, cards)
        if #cards ~= 1 then return nil end
        local card = Fk:cloneCard(self.pattern, cards.suit, cards.number)
        card.skillName = self.name
        card:addSubcards(cards)
        return card
    end,
    enabled_at_play = function(self, player)
        return player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    enabled_at_response = function(self, player, response)
        return false
    end,
})

huguoyuanjiao:addEffect(fk.TargetSpecified,{
    can_refresh = function(self, event, target, player, data)
        return data.card.skillName == "mcdh_huguoyuanjiao" and target == player
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local targets = AimGroup:getAllTargets(data.tos)
        local flag = true
        for _, pid in ipairs(targets) do
            if not player:inMyAttackRange(room:getPlayerById(pid)) then
                flag = false
            end
        end
        if flag then
            if not data.extraUse then
                player:addCardUseHistory(data.card.trueName, -1)
                data.extraUse = true
            end
        end
    end
})

huguoyuanjiao:addEffect('targetmod',{
    bypass_distances = function(self, player, skill, card, target)
        return card and table.contains(card.skillNames, "mcdh_huguoyuanjiao")
    end,
    bypass_times = function (self, player, skill, scope, card, to)
        if card and table.contains(card.skillNames, "mcdh_huguoyuanjiao") then
            return player:inMyAttackRange(to)
        end
    end
})

return huguoyuanjiao